package com.hmall.common.interceptor;

import com.hmall.common.utils.UserContext;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拿到请求
        String userInfo = request.getHeader("user-info");
        System.out.println("++++++++++++++拿到了用户的id"+userInfo);
        if(userInfo!=null){
            //把这个userInfo给转为long类型的
            Long userId = Long.valueOf(userInfo);
            //放到threadLocal里面
            UserContext.setUser(userId);
        }
        //不管有没有都放行
        return true;



    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    UserContext.removeUser();
    }
}
